<template>
  <div class="container" :style="{backgroundColor: site.template_color}">
    <div class="row" v-if="site">
      <div class="col-sx-12 col-md-8 offset-md-2 border-0 px-0">
        <a :href="site.top_image_link" v-if="site.top_image_src">
          <img v-on:load="imgLoad($event)" class="card-img-top" :src="site.top_image_src" alt="Card image cap">
        </a>
        <video class="w-100 mt-1" v-on:error="video_load_error($event)" autoplay controls :src="video_url" style="object-fit: fill;" v-if="video_url"></video>
        <div class="video-ads">
          <div v-for="ad in site.ads" class="my-1 text-center" :style="{border: ad.show_border ? '1px solid ' + ad.border_color : 'none' }">
            <a :href="ad.link" :class="{ flicker : ad.flicker }" :style="{ fontSize: ad.size + 'px', color: ad.color }">
              {{ad.title}}
            </a>
          </div>
        </div>
        <div class="qrcode w-100 mx-auto d-block text-center">
          <img :src="site.qrcode_link" class="rounded mw-100">
        </div>
        <div class="qrcode w-100 mx-auto d-block text-center">
          <p :class="{ flicker : configs.lianxi_flicker }">{{configs.lianxi_text}}:{{configs.qq}}</p>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
  import MtaH5 from 'mta-h5-analysis'
  export default {
    name: 'ViewSite',
    data() {
      return {
        logo: {
          src: '',
          title: 'VuejsCaff'
        },
        url: '',
        site: {

        },
        configs: {
          outtime: 5,
        },
        video_id: 0,
        video: {

        },
        video_links: [],
        video_url: '',
        load_count: 0,
        autoHeight: 'auto'
      }
    },
    beforeCreate() {
      console.log('修改生效')

    },
  created() {
    let _ = this,
        site_id = this.$route.query.site,
        url_meta = document.getElementsByTagName('meta')['api-url'],
        video_id = this.$route.query.video
    this.url = 'http://47.105.203.80'
    if( url_meta ) {
      _.url = url_meta.content
    }
    this.axios.get(_.url + '/api/site/' + site_id + '?video=' + video_id).then(res => {
      if (res.status === 200) {
        _.configs = res.data.configs
        _.site = res.data.site_info
        _.video = res.data.video
        _.video_url = _.video.link
        MtaH5.init({
          "sid":res.data.configs.sid,
          "cid":res.data.configs.cid,
          "autoReport":0,
          "senseHash":0,
          "senseQuery":0,
          "performanceMonitor":0,
          "ignoreParams":[]
        })
        document.title = _.site.title
        document.getElementsByTagName('body')[0] .style.backgroundColor = _.site.template_color
        this.tongji(_.configs.outtime, _.site.user_id)
      }
    })
    // console.log(MtaH5)
  },
  methods: {
    tongji(time, user_id) {
      setTimeout(function (){
        MtaH5.clickStat(user_id ,{})
      }, time * 1000);
    },
    get_all_video() {
      this.load_count++
      this.axios.get(this.url + '/api/video').then(res => {
        this.video_links = res.data;
        this.video_url = res.data;
      })
    },
    video_load_error(e) {
      if (this.load_count < 10) this.get_all_video()
    },
    imgLoad(e) {
      let img = new Image();

      img.onload = () => {
        this.autoHeight = img.width * 0.3125 + 'px'
      }
      img.src= this.site.top_image_src
    }
  }
}
</script>

<style scoped lang="scss">
.container {
  padding:0;
  margin:0 auto;
  .row {
    margin:0;
  }
}
.title { display: none; }
.card-img-top {
  width: 100%;
  height: 31.25vw;
}
a {
  &:hover {
    text-decoration: none;
  }
}
.flicker {
  animation: flicker 1.0s infinite;
}
@keyframes flicker{
  0% {
    opacity: 1;
  }
  100% {
    opacity: 0;
  }
}
</style>
